import os
from typing import Generator
import dolphindb as ddb
from gypb.config import settings
from contextlib import contextmanager
from xqdata_ddb import DDBDataApi
from xqdata_rq import RQDataApi


# 配置 DolphinDB 连接
DOLPHINDB_HOST = settings.database.host  # 使用服务名称作为主机名
DOLPHINDB_PORT = settings.database.port
USER = settings.database.user  # 替换为你的用户名
PASSWORD = settings.database.password  # 替换为你的密码
# 配置 rqdata账号密码 连接
RQ_USER = settings.rqdata.user
RQ_PASSWORD = settings.rqdata.password

@contextmanager
def get_db() -> Generator[ddb.Session, None, None]:
    session = ddb.Session(DOLPHINDB_HOST, DOLPHINDB_PORT, USER, PASSWORD)
    try:
        yield session
    finally:
        session.close()

def get_ddbapi():
    ddb_api = DDBDataApi()
    ddb_api.auth(DOLPHINDB_HOST, DOLPHINDB_PORT, USER, PASSWORD)
    return ddb_api

def get_rqapi():
    rq_api = RQDataApi()
    rq_api.auth(username=RQ_USER,password=RQ_PASSWORD)
    return rq_api

# def get_db() -> Generator[ddb.Session, None, None]:
#     with ddb.Session(DOLPHINDB_HOST, DOLPHINDB_PORT, USER, PASSWORD) as session:
#         yield session